<?php
class Khachhang_Form_ValidateKhcn{
	
	//Chua nhung thong bao loi cua form
	protected $_messagesError = null;
	
	//MANG CHUA DU LIEU SAU KHI KIEM TRA
	protected $_arrData;
	
	public function __construct($arrParam = array(), $options = null) {
		
		//=========================================
		//KIEM TRA name
		//=========================================
		$validator = new Zend_Validate();
		$validator	->addValidator(new Zend_Validate_NotEmpty(),true);
		
		if (!$validator->isValid($arrParam['name'])) {
			$message = $validator->getMessages();
			$this->_messagesError['name'] = current($message);
		}
		
		//=========================================
		//KIEM TRA birthday
		//=========================================
		if(!empty($arrParam['birthday'])){
			$validator = new Zend_Validate();
			$validator	->addValidator(new Zend_Validate_Date(array('format'=>'dd/mm/YYYY')),true);
			
			if(!$validator->isValid($arrParam['birthday'])){
				$message = $validator->getMessages();
				$this->_messagesError['birthday'] = current($message);
			}
		}
		
		if(!empty($arrParam['ngaycap'])){
			$validator = new Zend_Validate();
			$validator	->addValidator(new Zend_Validate_Date(array('format'=>'dd/mm/YYYY')),true);
				
			if(!$validator->isValid($arrParam['ngaycap'])){
				$message = $validator->getMessages();
				$this->_messagesError['ngaycap'] = current($message);
			}
		}
		
		//=========================================
		//KIEM TRA email
		//=========================================
		if(!empty($arrParam['email'])){
			
			if($arrParam['action'] == 'add'){
				$options = array('table'=>'khachhang','field'=>'email');
			}else if($arrParam['action'] == 'edit'){
				$clause = ' id !=' . $arrParam['id'];
				$options = array('table'=>'khachhang','field'=>'email','exclude'=>$clause);
			}
			$validator = new Zend_Validate();
			$validator	-> addValidator(new Zend_Validate_EmailAddress(),true)
						-> addValidator(new Zend_Validate_Db_NoRecordExists($options),true);
			
			if(!$validator->isValid($arrParam['email'])){
				$message = $validator->getMessages();
				$this->_messagesError['email'] = current($message);
			}
		}
		
		//=========================================
		//KIEM TRA cmnd
		//=========================================
		if(!empty($arrParam['cmnd'])){
				
			if($arrParam['action'] == 'add'){
				$options = array('table'=>'khachhang_cn','field'=>'cmnd');
			}else if($arrParam['action'] == 'edit'){
				$clause = ' id !=' . $arrParam['id'];
				$options = array('table'=>'khachhang_cn','field'=>'cmnd','exclude'=>$clause);
			}
			$validator = new Zend_Validate();
			$validator	-> addValidator(new Zend_Validate_Db_NoRecordExists($options),true);
				
			if(!$validator->isValid($arrParam['cmnd'])){
				$message = $validator->getMessages();
				$this->_messagesError['cmnd'] = current($message);
			}
		}
		
		//=========================================
		//KIEM TRA phone
		//=========================================
		if(!empty($arrParam['phone'])){
		
			if($arrParam['action'] == 'add'){
				$options = array('table'=>'khachhang','field'=>'phone');
			}else if($arrParam['action'] == 'edit'){
				$clause = ' id !=' . $arrParam['id'];
				$options = array('table'=>'khachhang','field'=>'phone','exclude'=>$clause);
			}
			$validator = new Zend_Validate();
			$validator	-> addValidator(new Zend_Validate_Db_NoRecordExists($options),true);
		
			if(!$validator->isValid($arrParam['phone'])){
				$message = $validator->getMessages();
				$this->_messagesError['phone'] = current($message);
			}
		}
		
		//=========================================
		//KIEM TRA id_phongban
		//=========================================
		if ($arrParam['id_phongban'] == 0) {
			$this->_messagesError['id_phongban'] = 'Bạn cần chọn đơn vị quản lý';
		}
		
		//=========================================
		//KIEM TRA id_nhanvien
		//=========================================
		if ($arrParam['id_nhanvien'] == 0) {
			$this->_messagesError['id_nhanvien'] = 'Bạn cần chọn người quản lý';
		}
		
		
		//=========================================
		//TRUYEN CAC GIA TRI DUNG VAO MANG $_arrData
		//=========================================
		$this->_arrData = $arrParam;
		
	}
	
	//Kiem tra Error
	//return true neu co loi xuat hien
	public function isError() {
		if (count($this->_messagesError) > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	//Tra ve mot mang cac thong bao loi
	public function getMessageError() {
		return $this->_messagesError;
	}
	
	//Tra ve mot du lieu sau khi kiem tra
	public function getData($options = null) {
		return $this->_arrData;
	}
	
}


